package com.web.action;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.web.action.res.Response;
import com.web.common.Sys;
import com.web.model.PageBean;
import com.web.model.TDict;
import com.web.model.TUser;
import com.web.model.constant.Constant;
import com.web.service.TDictService;

@Controller
@RequestMapping("dict")
public class TDictAction {
	
	@Autowired
	private TDictService dictService;
	
	@Resource(name="sys")
	private Sys sys;
	
	@RequestMapping(value="dict_list",method=RequestMethod.GET)
	public String dictList(HttpServletRequest request,HttpSession session){
		TUser u = (TUser) request.getSession().getAttribute(Constant.SESSION_USER);
		if (u == null) { // 用户未登录
			return "login";
		}
		Map<String, Object> param = new HashMap<String, Object>();
		String page = (request.getParameter("page") == null ? "1" : request.getParameter("page"));
		param.put("dictKey", "PK_CAT_IND");
		param.put("pageNum", Integer.parseInt(page));
		param.put("pageSize", 30);
		param.put("sort", "dict_type,dict_code");
		param.put("order", Constant.ORDER_ASC);
		PageBean<TDict> pageBean = dictService.selectListByPage(param);
		pageBean.setUrl("/dict/dict_list?page=");
		request.setAttribute("pageBean", pageBean);
		pageBean.getPageStr();
		return "manage/double_dict_list";
	}
	
	@RequestMapping(value="update_dict_rate",method=RequestMethod.POST)
	@ResponseBody
	public Response updateDictRate(TDict dict,HttpServletRequest request,HttpSession session){
		Response res = new Response();
		TUser u = (TUser) request.getSession().getAttribute(Constant.SESSION_USER);
		if (u == null) { // 用户未登录
			return res.TokenInvalid;
		}else if(u.getRoleId()!=1){
			return res.ParamError;
		}
		if(dict.getDictRate() == null || dict.getDictId() == null){
			return res.ParamError;
		}
		dictService.updateByPrimaryKey(dict);
		
		sys.loadDicts();//刷新缓存中的dict数据
		return res.SUCC;
	}
}
